import {
    getToken
} from '@/util/auth'
import {
    imgUrl
} from '../filters/index'
const classNames = ['error', 'success', 'primary', 'info', 'error', 'warning'];
import Store from '../store/';
// const allPermission = Store.state.user.permission;
const isAdmin = Store.state.user.userInfo.superAdmin;
 
export default {
    data() {
        return {
            UPLOAD_URL: '/admin/upload/image'
        }
    },
    methods: {
        imgUrl,
        isAdmin() {
            return isAdmin;
        },
        showSuccess(msg) {
            this.$message({
                type: 'success',
                message: msg || '操作成功!'
            })
        },
        parseCellClassNameByValue(value) {
            return 'cell-' + classNames[value] || ''
        },
        beforeAvatarUpload() {

        },
        getHeaders() {
            return {
                Authorization: getToken(),
            }
        },
        handleAvatarSuccess(imgField) {
            return (res, file) => {
                if (res && res.code === 200) {
                    // this.temp[imgField] = res.data
                    this.$set(this.temp, imgField, res.data.url)
                }
            }
        },
        parseCellClassNameByDicData(value, dicData) {
            for (let i = 0; i < dicData.length; i++) {
                const dicItem = dicData[i];
                if (value === dicItem.value && dicItem.type) {
                    return 'cell-' + dicItem.type;
                }
            }
            return '';
        },
        success(msg = '操作成功') {
            this.$notify({
                title: '提示',
                message: msg,
                type: 'success'
            })
        },
        confirm(msg) {
            return this.$confirm(msg, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
        },
        requestToken() {
            return {
                Authorization: getToken()
            }
        },
        alert(content, callback = () => { }) {
            this.$alert(content, '提示', {
                confirmButtonText: '确定',
                callback: callback
            });
        },
        showWarning(msg) {
            this.$message({
                type: 'warning',
                message: msg
            })
        },
        showNotify(msg) {
            this.$notify({
                title: '成功',
                message: msg || '操作成功',
                type: 'success',
                duration: 2000
            })
        },
        openWin(newUrl) {
            let iframe = document.createElement('iframe')
            iframe.style.display = 'none'
            let form = document.createElement('form')
            form.action = newUrl
            form.method = 'get'
            form.target = '_self'
            iframe.appendChild(form)
            document.body.appendChild(iframe)
            form.submit()
            setTimeout(() => {
                iframe.parentElement.removeChild(iframe)
            }, 100)
        },
        parseArr2Params(keyname, arr) {
            let rt = {}
            for (let i = 0; i < arr.length; i++) {
                let keyPrefix = keyname + '[' + i + ']'
                let row = arr[i]
                if (typeof row === 'object') {
                    Object.keys(row).forEach(key => {
                        rt[keyPrefix + '.' + key] = row[key]
                    })
                } else {
                    rt[keyPrefix] = row
                }
            }
            return rt
        },
        showError(msg) {
            this.$message({
                type: 'error',
                message: msg
            })
        },
        showConfirm(msg, sureCallback) {
            this.$confirm(msg, '提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(sureCallback).catch(() => { })
        }
    }
}
